package io.le.starter.lock.config;

import io.etcd.jetcd.Client;
import io.le.starter.lock.locker.manager.LockFactory;
import io.le.starter.lock.locker.manager.LockManager;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * createTime：2025-04-07 15:58
 * description：
 */

@Configuration
@EnableConfigurationProperties({LockProperties.class})
public class LockAutoConfig {

    @Autowired
    public LockProperties lockProperties;


    @Bean("zookeeper")
    public ZooKeeper zooKeeper() {
        try {
            return new ZooKeeper(lockProperties.getDefaultZKUrl(), 10000, null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Bean("etcdClient")
    public Client etcdClient() {
        return Client.builder().endpoints(lockProperties.getDefaultEtcdUrl()).build();

    }

    @Bean("lockFactory")
    public LockFactory lockFactory() {
        return new LockFactory();

    }

    @Bean("lockManager")
    public LockManager lockManager() {
        return new LockManager(lockProperties.getDefaultLockType());

    }


}
